import { Toast } from 'vant'
import {interfaceUtils} from './interface.js'
let crypto = require('crypto');
// https://www.npmjs.com/package/jsencrypt?activeTab=versions
import JsEncrypt from "jsencrypt";

class ReceivePiece extends interfaceUtils {
    static IsRequire=22
    static Name="receivePiece"
    static TimeStamp=new Date().getTime()
    constructor(
        weight ,consumerid,waybillnumber,shipprovince,shipcity,shipzone,receiveprovince ,receivecity ,receivezone ,estimatedfreightcharge  , 
        shipaddress ,receiveaddress ,shipmember ,shipmemberphone ,receivemember ,receivememberphone ,picktime ,itemtype ,phone,counts,payType,deliveryTypeId   
        ){
        super()
        this.weight=weight
        this.consumerId=consumerid ||'null'
       


        this.shipProvince =shipprovince
        this.shipCity =shipcity
        this.shipZone =shipzone

        this.receiveProvince =receiveprovince
        this.receiveCity =receivecity
        this.receiveZone =receivezone

        this.estimatedFreightCharge =estimatedfreightcharge
        this.receiveFrom  =ReceivePiece.receivefrom


        this.shipAddress =shipaddress
        this.receiveAddress =receiveaddress

        this.shipMember =shipmember
        this.shipMemberPhone =shipmemberphone

        this.receiveMember =receivemember
        this.receiveMemberPhone =receivememberphone

        this.pickTime =picktime

        this.itemType =itemtype

        this.phone=phone ||'null'
        this.counts=counts || 'null'
        this.payType=payType
        this.deliveryTypeId=deliveryTypeId
        this.waybillNumber =waybillnumber

        this.timeStamp=new Date().getTime()

    }
    _getParams(){
        const publicKey=ReceivePiece.publicKey
        let rsa = new JsEncrypt();
        rsa.setPublicKey(publicKey);
        const {  weight ,
            consumerId,
            waybillNumber,            
            shipCity,                    
            receiveCity ,           
                  
        } =this
        // console.log(loginPassword)
        const consumerFrom=ReceivePiece.consumerFrom      
        const params=JSON.stringify( { 
            weight ,
            consumerId,
            waybillNumber,            
            shipCity,                    
            receiveCity ,        
             
                        
            }) 
        const result= rsa.encrypt(params)  
         //    https://www.cnblogs.com/aeolian/p/12133598.html
        return encodeURIComponent(result)      
     }
     _getSign(){
        const key =ReceivePiece.md5Key+""
        const {phone} =this 
        const consumerFrom=ReceivePiece.consumerFrom+""
        const {timeStamp}=this
     //    https://blog.csdn.net/wail521/article/details/96430873       
        //  const sign  = this._parmaSort({ key ,phone,consumerFrom ,timeStamp  }) 
        const sign  = phone +consumerFrom+timeStamp+key
         const result = crypto.createHash('md5').update(sign).digest("hex")    
         return result 
     }
     _getOther(){
        const { shipAddress ,receiveAddress ,shipMember ,shipMemberPhone ,receiveMember ,receiveMemberPhone ,pickTime ,itemType,payType,counts ,
            shipProvince,
            shipZone,
            receiveProvince ,
            receiveZone ,
            receiveFrom,estimatedFreightCharge,deliveryTypeId } =this    
       
        return JSON.stringify( { shipAddress ,receiveAddress ,shipMember ,shipMemberPhone ,receiveMember ,receiveMemberPhone ,pickTime ,itemType,payType,counts ,
            shipProvince,
            shipZone,
            receiveProvince ,
            receiveZone ,
            receiveFrom,estimatedFreightCharge,deliveryTypeId })


     }
 
     _getData(){
       
         const params=this._getParams()
         const sign=this._getSign()
         const other=this._getOther()
         const {phone} = this
         const consumerFrom=ReceivePiece.consumerFrom
         const {timeStamp}=this
 
         let formData = new FormData();
         formData.append("params",params);
         formData.append("sign",sign);
         formData.append("other",other);
         formData.append("phone",phone);
         formData.append("consumerFrom",consumerFrom);
         formData.append("timeStamp",timeStamp);
 
 
 
         return  formData
 
 
     }
 
       /**
      * 
      * @param {keyObj} 将对象排序 变成 字符串
      *  例如 a=1&b=2 
      */
     _parmaSort(keyObj){
         let keyArr = [];
       for (let key in keyObj) {
         keyArr.push(key);
       }
         let reaKryArr = keyArr.sort();
         let newArrStr = "";
         for (let i = 0; i < keyArr.length; i++) {
           newArrStr += reaKryArr[i] + "=" + keyObj[reaKryArr[i]] + "&";
         }
     
         let str = newArrStr.substr(0, newArrStr.length - 1);
         return str
     }
}
//对weight 单独校验
export function handlerWeight(data){
    let flg=true
    let str=data+""
    // str.slice(0,1) == '0'  || 
    if( str.indexOf(" ") >=0){
        Toast.fail("不能输入空格");
        flg= false
    }
    if(str.slice(0,1) == '0'){
        Toast.fail("不能以0开头");
        flg= false
    }
    return flg

}

//不能为 数字 0 是 数字0 返回 false
export function qualified(data){
    if(data == null || data ==""||data == undefined ){
        return false
    }else{
        return true
    }
}

// console.log(qualified(10)) 


export function validatas(data,class_type){
    let qualify =false

    let set= Object.entries(data).slice(0,class_type.IsRequire)
    // console.log('set', set)
    let ret= set.find((value,index)=>{
        return !qualified( value[1])
    })
    if( ret == undefined){  
        //符合要求   
        if( 
            qualified(data.counts )  &&  qualified(data.weight ) &&   qualified(data.consumerId  )  &&  
            qualified(data.shipProvince  )  &&  qualified(data.shipCity  ) &&  qualified(data.shipZone )  &&  qualified(data. receiveProvince  ) &&
            qualified(data.receiveCity  )  &&  qualified(data.receiveZone  ) &&  qualified(data.estimatedFreightCharge  )  &&  qualified(data.receiveFrom   ) &&
            qualified(data.shipAddress  )  &&  qualified(data.receiveAddress  ) &&  qualified(data.shipMember  )  &&  qualified(data.shipMemberPhone  ) &&
            qualified(data.receiveMember  )  &&  qualified(data.receiveMemberPhone  ) &&  qualified(data.pickTime  )  &&  qualified(data.itemType  ) &&
            qualified(data.phone )  &&   qualified(data.payType )    && handlerWeight(data.weight)
        ){    
            // switch (class_type.Name) {
            //     case 'receivePiece':
            //         if((data.loginPassward+"").length<6 || (data.loginPassward+"").length>20){
            //             Toast.fail({
            //                 message:"请输入6-20位密码"
            //             })
            //             qualify=false
            //         }else{
            //             qualify=true
            //         }       
            //      break;               
            // }     
            qualify=true   
              return qualify
            }else{             
                return qualify
            }     
    }else{
       Toast.fail(ret[0] +" is Null");
        return false   
    }   
 


    
}


export { ReceivePiece }